




//https://www.ozon.ru/api/entrypoint-api.bx/page/json/v2?url=/product/1185794392/?layout_container=pdpPage2column&layout_page_index=2
function getQueryString(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return r[2];
    };
    return 0;
 }



console.log(" get shein detail ok ");

var auto_get = (getQueryString("auto_get"))
console.log(auto_get);


if(auto_get ==="true"){
   /* setTimeout(function() {
        window.close();
    }, 20000);
*/
    $(document).ready(function() {

        setTimeout(() => {
            smoothScrollTo(500);
        }, Math.random() * 2000); // 在2秒内随机延迟开始滚动


        setTimeout(() => {


            if ($("body").text().indexOf("抱歉，該商品已被刪除。") !== -1) {
                console.log("页面上存在 '删除' 文案");
                var type = "sheindetaildel";
                var data={}
                data.id = getQueryString("bid");
                chrome.runtime.sendMessage({ data: {type,data }, close: false })
                console.log(data);
            }


            var have_data = false;
            // 选择所有的 <script> 标签
            $('script').each(function() {
                let scriptContent = $(this).html(); // 获取脚本内容
                const regex = /window\.gbRawData\s*=\s*(.*?)(\n|\r\n)/;  // 匹配结尾为 ; 或换行符
                // 使用正则表达式匹配
                const match = scriptContent.match(regex);
                if (match) {
                    try {
                        // 用 JSON.parse 提取数据
                        let gbRawData = JSON.parse(match[1]);
                        console.log("Extracted gbRawData:", gbRawData);
                        if(location.href.includes("www.shein.com.hk")){
                            getDataDetailPageV2(gbRawData,getQueryString("bid"));
                        }else{
                            getPriceAndStockFromDetailPage(gbRawData,getQueryString("bid"));
                        }

                    } catch (e) {
                        console.error("Error parsing JSON:", e);
                    }
                    have_data = true;
                }
            });

            if(have_data===false){
                let eror_html = $(".c-error-404").html();
                console.log(eror_html);
                if(eror_html){
                    var type = "sheindetaildel";
                    var data={}
                    data.id = getQueryString("bid");
                    console.log("sheindetaildel------");
                    chrome.runtime.sendMessage({ data: {type,data }, close: false })
                    console.log(data);
                }

            }

        },2000);


    });
}




function smoothScrollTo(targetScroll) {
    const startScroll = window.scrollY || document.documentElement.scrollTop;
    const distance = targetScroll - startScroll;
    const duration = Math.floor(Math.random() * 1000) + 1000; // 随机持续时间 1000 到 2000 毫秒
    const startTime = performance.now();

    // 鼠标移动模拟
    let mouseMovementInterval;

    function scrollStep(currentTime) {
        const elapsedTime = currentTime - startTime;
        const progress = Math.min(elapsedTime / duration, 1);

        // 使用缓动函数来平滑滚动
        const easeInOutQuad = (t) => t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;

        const scrollValue = startScroll + distance * easeInOutQuad(progress);
        window.scrollTo(0, scrollValue);

        if (elapsedTime < duration) {
            requestAnimationFrame(scrollStep);
        } else {
            clearInterval(mouseMovementInterval); // 滚动结束后清除鼠标移动模拟
        }
    }

    // 鼠标移动模拟
    function simulateMouseMovement() {
        const randomX = Math.random() * window.innerWidth;
        const randomY = Math.random() * window.innerHeight;

        const mouseEvent = new MouseEvent('mousemove', {
            clientX: randomX,
            clientY: randomY,
            bubbles: true,
            cancelable: true
        });

        document.dispatchEvent(mouseEvent);
    }

    // 获取随机的时间间隔
    function getRandomInterval() {
        return Math.floor(Math.random() * 1000) + 300; // 随机300到1300毫秒
    }

    // 初始化鼠标移动间隔
    mouseMovementInterval = setInterval(() => {
        simulateMouseMovement();
    }, getRandomInterval());

    // 开始滚动
    requestAnimationFrame(scrollStep);
}
